import 'package:flutter/material.dart';

abstract class BaseWidget extends StatefulWidget {
  const BaseWidget({Key? key}) : super(key: key);

  @protected
  @factory
  BaseWidgetState attachState();

  @override
  State<StatefulWidget> createState() {
    return attachState(); // ignore: no_logic_in_create_state, this is the original sin
  }
}

abstract class BaseWidgetState<T extends BaseWidget> extends State<T>
    with AutomaticKeepAliveClientMixin {
  bool _isAppBarShow = true;

  bool _isErrorWidgetShow = false;

  String _errorContentMsg = "网络请求失败，请检查您的网络";
  String _errorImgPath = "assets/images/ic_error.png";

  bool _isLoadingWidgetShow = false;

  bool _isEmptyWidgetShow = false;
  String _emptyContentMsg = "暂无数据";
  String _emptyImgPath = "assets/images/ic_empty.png";

  bool _isShowContent = true;

  FontWeight _fontWeight = FontWeight.w600;

  @override
  bool get wantKeepAlive => true;

  @override
  void didUpdateWidget(covariant T oldWidget) {
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
        resizeToAvoidBottomInset: false,
        appBar: _attachBaseAppBar(),
        body: Stack(
          children: <Widget>[
            _attachBaseContentWidget(context),
            _attachBaseEmptyWidget(),
            _attachBaseErrorWidget(),
            _attachBaseLoadingWidget()
          ],
        ),
        floatingActionButton: fabWidget());
  }

//悬浮按钮
  Widget? fabWidget() {
    return null;
  }

  //导航栏
  AppBar? attachAppBar() {
    return null;
  }

  //暴露内容视图¸
  Widget attachContentWidget(BuildContext context);

  //错误页面点击事件
  void onClickErrorWidget() {}

  Widget _attachBaseContentWidget(BuildContext context) {
    return Offstage(
        child: attachContentWidget(context), offstage: !_isShowContent);
  }

//暴露错误页面方法，可以重写定制
  Widget attachErrorWidget() {
    return Container(
      padding: const EdgeInsets.fromLTRB(0, 0, 0, 80),
      width: double.infinity,
      height: double.infinity,
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Image(image: AssetImage(_errorImgPath), width: 120, height: 120),
            Container(
              margin: const EdgeInsets.fromLTRB(0, 20, 0, 0),
              child: Text(
                _errorContentMsg,
                style: TextStyle(color: Colors.grey, fontWeight: _fontWeight),
              ),
            ),
            Container(
              margin: const EdgeInsets.fromLTRB(0, 0, 20, 0),
              child: OutlinedButton(
                child: Text(
                  "重新加载",
                  style: TextStyle(color: Colors.grey, fontWeight: _fontWeight),
                ),
                onPressed: () => {onClickErrorWidget()},
              ),
            )
          ],
        ),
      ),
    );
  }

  Widget _attachBaseErrorWidget() {
    return Offstage(offstage: !_isErrorWidgetShow, child: attachErrorWidget());
  }

  Widget attachLoadingWidget() {
    return const Center(child: CircularProgressIndicator(strokeWidth: 2.0));
  }

  Widget _attachBaseLoadingWidget() {
    return Offstage(
        offstage: !_isLoadingWidgetShow, child: attachLoadingWidget());
  }

  Widget attachEmptyWidget() {
    return Container(
      padding: const EdgeInsets.fromLTRB(0, 0, 0, 100),
      width: double.infinity,
      height: double.infinity,
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Image(
                image: AssetImage(_emptyImgPath),
                width: 150,
                height: 150,
                color: Colors.black12),
            Container(
              margin: const EdgeInsets.fromLTRB(0, 10, 0, 0),
              child: Text(
                _emptyContentMsg,
                style: TextStyle(color: Colors.grey, fontWeight: _fontWeight),
              ),
            )
          ],
        ),
      ),
    );
  }

  Widget _attachBaseEmptyWidget() {
    return Offstage(offstage: !_isEmptyWidgetShow, child: attachEmptyWidget());
  }

  PreferredSizeWidget _attachBaseAppBar() {
    return PreferredSize(
        child: Offstage(
          offstage: !_isAppBarShow && attachAppBar() != null,
          child: attachAppBar(),
        ),
        preferredSize: const Size.fromHeight(56));
  }

  Future setErrorContent(String content) async {
    setState(() {
      _errorContentMsg = content;
    });
  }

  Future setErrorImg(String imgPath) async {
    setState(() {
      _errorImgPath = imgPath;
    });
  }

  Future setEmptyContent(String content) async {
    setState(() {
      _emptyContentMsg = content;
    });
  }

  Future setEmptyImg(String imgPath) async {
    setState(() {
      _emptyImgPath = imgPath;
    });
  }

  Future showContent() async {
    setState(() {
      _isShowContent = true;
      _isEmptyWidgetShow = false;
      _isErrorWidgetShow = false;
      _isLoadingWidgetShow = false;
    });
  }

  Future showLoading() async {
    setState(() {
      _isShowContent = false;
      _isEmptyWidgetShow = false;
      _isErrorWidgetShow = false;
      _isLoadingWidgetShow = true;
    });
  }

  Future showEmpty() async {
    setState(() {
      _isShowContent = false;
      _isEmptyWidgetShow = true;
      _isErrorWidgetShow = false;
      _isLoadingWidgetShow = false;
    });
  }

  Future showError() async {
    setState(() {
      _isShowContent = false;
      _isEmptyWidgetShow = false;
      _isErrorWidgetShow = true;
      _isLoadingWidgetShow = false;
    });
  }

  Future setAppBarShow(bool visible) async {
    setState(() {
      _isAppBarShow = visible;
    });
  }

  Future setFontWeight(FontWeight fontWeight) async {
    setState(() {
      _fontWeight = fontWeight;
    });
  }
}
